use std::collections::HashMap;

#[derive(Debug)]
struct RetirementPolicy {
    original_retirement_age: f64,   // 原始退休年龄（岁）
    policy_start_year: i32,         // 政策开始年份
    policy_start_month: i32,        // 政策开始月份
    delay_interval_months: i32,     // 延迟间隔（月）
    delay_months_per_interval: i32, // 每个间隔延迟的月数
    max_delay_months: i32,          // 最大延迟月数
}

fn calculate_retirement(
    birth_date_str: &str,
    person_type: &str,
    policies: &HashMap<&str, RetirementPolicy>,
) -> Option<(String, f64, i32)> {

    let birth_parts: Vec<&str> = birth_date_str.split('-').collect();
    if birth_parts.len() != 2 {
        return None;
    }
    let birth_year: i32 = birth_parts[0].parse().ok()?;
    let birth_month: i32 = birth_parts[1].parse().ok()?;

    let policy = policies.get(person_type)?;
    let mut retirement_year = birth_year + policy.original_retirement_age.floor() as i32;
    let mut retirement_month = birth_month
        + ((policy.original_retirement_age - policy.original_retirement_age.floor()) * 12.0).round()
            as i32;
    while retirement_month > 12 {
        retirement_year += 1;
        retirement_month -= 12;
    }
    let mut delayed_months = 0;
    let retirement_total_months = retirement_year * 12 + retirement_month;
    let policy_start_total_months = policy.policy_start_year * 12 + policy.policy_start_month;

    if retirement_total_months >= policy_start_total_months {
        let elapsed_months = retirement_total_months - policy_start_total_months;
        let intervals = (elapsed_months) / policy.delay_interval_months + 1;
        delayed_months = intervals * policy.delay_months_per_interval;
        if delayed_months > policy.max_delay_months {
            delayed_months = policy.max_delay_months;
        }
        retirement_month += delayed_months;

        while retirement_month > 12 {
            retirement_year += 1;
            retirement_month -= 12;
        }
    }

    let total_months = (retirement_year - birth_year) * 12 + (retirement_month - birth_month);
    let retirement_age = total_months as f64 / 12.0;
    let retirement_date_str = format!("{:04}-{:02}", retirement_year, retirement_month);

    Some((
        retirement_date_str,
        (retirement_age * 100.0).round() / 100.0,
        delayed_months,
    ))
}

pub fn retire_time(time: &str, tp: &str) -> String {
    let mut policies: HashMap<&str, RetirementPolicy> = HashMap::new();

    policies.insert(
        "原法定退休年龄55周岁女职工",
        RetirementPolicy {
            original_retirement_age: 55.0,
            policy_start_year: 2025,
            policy_start_month: 1,
            delay_interval_months: 4,
            delay_months_per_interval: 1,
            max_delay_months: ((58.0 - 55.0) * 12.0) as i32,
        },
    );
    policies.insert(
        "原法定退休年龄50周岁女职工",
        RetirementPolicy {
            original_retirement_age: 50.0,
            policy_start_year: 2025,
            policy_start_month: 1,
            delay_interval_months: 2,
            delay_months_per_interval: 1,
            max_delay_months: ((55.0 - 50.0) * 12.0) as i32,
        },
    );
    policies.insert(
        "男职工",
        RetirementPolicy {
            original_retirement_age: 60.0,
            policy_start_year: 2025,
            policy_start_month: 1,
            delay_interval_months: 4,
            delay_months_per_interval: 1,
            max_delay_months: ((63.0 - 60.0) * 12.0) as i32,
        },
    );

    if let Some((retirement_date, retirement_age, delayed_months)) =
        calculate_retirement(time, tp, &policies)
    {
        if retirement_age.fract() == 0.0 {
            format!(
                "{},{:.0},{}",
                retirement_date, retirement_age, delayed_months
            )
        } else {
            format!(
                "{},{:.2},{}",
                retirement_date, retirement_age, delayed_months
            )
        }
    } else {
        "输入有误或未知的人员类型".to_string()
    }
}
